<?php
require('./fpdf.php');
include("include/conn.php");
{
  $billBookDetail = array();
  $total          = array();
  $partyId        = 0;
  $totNetWeight   = 0;
  $billBookNo     = 0;
  $showPrev       = 0;
  $partyName      = "";
  $total['DrFine']   = 0;
  $total['DrQty']    = 0;
  $total['DrLabour'] = 0;
  $total['DrAmount'] = 0;
  $total['CrFine']   = 0;
  $total['CrQty']    = 0;
  $total['CrLabour'] = 0;
  $total['CrAmount'] = 0;
  $previousFine   = 0;
  $previousAmount = 0;
  $totalFine      = 0;
  $totalAmount    = 0;
  $prevFine       = 0;
  $prevAmount     = 0;
  $amount         = 0;
  $fine           = 0;
  
  $pdf = new FPDF('P','mm','A4');
  $pdf->AliasNbPages();
  $pdf->AddPage();
  
  $selectParty = "SELECT party.partyId,party.partyName,billbook.billBookId,billbook.billNo,DATE_FORMAT(billbook.billBookDate,'%d-%m-%Y') AS billBookDate,
                         billbook.totalFine,billbook.totalAmount,billbook.showPrev,billbook.prevFine,billbook.prevAmount
                    FROM party
                    JOIN billbook ON billbook.partyId = party.partyId
                   WHERE billbook.billBookId = ".$_GET['billBookId'];
  $selectPartyRes = mysql_query($selectParty);
  if($partyRow = mysql_fetch_assoc($selectPartyRes))
  {
	  $previousFine   = $partyRow['prevFine'];
	  $previousAmount = $partyRow['prevAmount'];
	  $totalFine      = $partyRow['totalFine'] + $previousFine;
    $totalAmount    = $partyRow['totalAmount'] + $previousAmount;
  }
  
  // Query for previous Fine and Amount : Starts
  $fineBill = 0;
  $amountBill = 0;
	$selectBillBook = "SELECT prevFine,prevAmount
	                     FROM billbook
	                    WHERE partyId = ".$_GET['partyId'];
	$selectBillBookRes = mysql_query($selectBillBook);
	while($row = mysql_fetch_array($selectBillBookRes))                   
	{
	  $fineBill   += $row['prevFine'];
	  $amountBill += $row['prevAmount'];
	}
   
  $selectBillBook = "SELECT billbookdetail.particulars,
                            billbookdetail.weight,billbookdetail.pari,billbookdetail.netWeight,billbookdetail.touch,billbookdetail.waste,billbookdetail.fine,
                            billbookdetail.particular,billbookdetail.qty,billbookdetail.labour,billbookdetail.amount 
                       FROM billbookdetail 
                      WHERE billbookdetail.billBookId = ".$_GET['billBookId']."
                      ORDER BY billbookdetail.billBookDetailId";
  $selectBillBookRes = mysql_query($selectBillBook);
  $a = 0;
  $total['netWeight'] = 0;
  $total['fine']      = 0;
  $total['qty']       = 0;
  $total['labour']    = 0;
  $total['amount']    = 0;
  $totalNetWeight     = 0;
  $totFine            = 0;
  $totAmount          = 0;
  $remainRow          = 0;
  while($dataRow = mysql_fetch_array($selectBillBookRes))
  {
      $totalNetWeight += $dataRow['netWeight'];
      $totFine        += $dataRow['fine'];
      $totAmount      += $dataRow['amount'];
      
      if(($a % $cfgRecPerGrnPage) == 0)
      { 
        $yPosition = 55;
        if($a > 0)
          $pdf->AddPage();
        pageHeader();
        billMasterFunction();
        billDetailHeaderFunction(); 
        border();
      }
      billDetailFunction($yPosition,$a+1); 
   
      $yPosition += 5;
      $a++;
  }
  
  $billBook = "SELECT billBookId,totalFine,totalAmount,prevFine,prevAmount,showPrev 
                 FROM billbook
                WHERE partyId = ".$partyRow['partyId']."
                  AND billBookId = ".$_GET['billBookId']."
                ORDER BY billBookId";
  $billBookRes = mysql_query($billBook);
  while($billBook = mysql_fetch_assoc($billBookRes))
  {
    $showPrev   = $billBook['showPrev'];
    if($showPrev != 0)
      previousNet();
  }
  
  while($a < $cfgRecPerGrnPage)
  {	
		remainRowFunction($yPosition);
		$yPosition += 5;
		$a++;
  }
	totalBillDetail();
	
	$pdf->output();
	include("./bottom.php");
}

function pageHeader()
{
  global $pdf;
  $pdf->Image('./images/shreeSava.jpg',1,2,37,20);
  $pdf->Image('./images/shreeGanesh.jpg',87,2,30,30);
  $pdf->Image('./images/shreeGelMataji.jpg',170,2,37,20);
  $pdf->SetLineWidth(0.01);
}

function billMasterFunction()
{
	 global $pdf;
	 global $partyRow;
	 $pdf->SetLineWidth(0.01); 
   $pdf->SetXY(4,30);
   $pdf->SetFont('Arial','',13);
   $pdf->Write(5,'Party : '.$partyRow['partyName']);	 
   $pdf->SetXY(4,40);
   $pdf->SetFont('Arial','',13);
   $pdf->Write(5,'Rough Purchase');	 
   $pdf->SetXY(80,30);
   $pdf->SetFont('Arial','',13);
   $pdf->Write(5,'Date : '.$partyRow['billBookDate']);	 
   $pdf->SetXY(170,30);
   $pdf->SetFont('Arial','',13);
   $pdf->Write(5,'BillNo : '.$partyRow['billNo']);	 
}

function billDetailHeaderFunction()
{
  global $pdf;
  $pdf->SetFont('Arial', 'B', 10);
  $pdf->SetXY(6,50);
  $pdf->cell(30,05,'PARTICULAR','1', '0', 'L');
 
  $pdf->SetXY(36,50);
  $pdf->cell(15,05,'WEI.','1', '0', 'L');
  
  $pdf->SetXY(51,50);
  $pdf->cell(15,05,'PANI.','1', '0', 'L');
  
  $pdf->SetXY(66,50);
  $pdf->cell(18,05,'N.W','1', '0', 'C');

  $pdf->SetXY(84,50);
  $pdf->cell(20,05,'TOUC.','1', '0', 'L');

  $pdf->SetXY(104,50);
  $pdf->cell(15,05,'WAST.','1', '0', 'L');

  $pdf->SetXY(119,50);
  $pdf->cell(15,05,'FINE.','1', '0', 'L');

  $pdf->SetXY(134,50);
  $pdf->cell(20,05,'PARTI.','1', '0', 'L');
  
  $pdf->SetXY(154,50);
  $pdf->cell(15,05,'QTY','1', '0', 'L');
  
  $pdf->SetXY(169,50);
  $pdf->cell(20,05,'LAB','1', '0', 'L');
  
  $pdf->SetXY(189,50);
  $pdf->cell(17,05,'AMOUNT','1', '0', 'L');
}
function billDetailFunction($yPosition)
{
	global $pdf;
	global $dataRow;
	$pdf->SetFont('Arial', '', 10);
	$pdf->SetXY(6,$yPosition);
  $pdf->cell(30,05,$dataRow['particulars'],'1', '0', 'L');
	
	$pdf->SetXY(36,$yPosition);
	if($dataRow['weight'] == 0)
	  $pdf->cell(15,05,"",'1', '0', 'L');
	else
	  $pdf->cell(15,05,$dataRow['weight'],'1', '0', 'R');
	
	$pdf->SetXY(51,$yPosition);
	if($dataRow['pari'] == 0)
	  $pdf->cell(15,05,"",'1', '0', 'L');
	else
	  $pdf->cell(15,05,$dataRow['pari'],'1', '0', 'R');
	
	$pdf->SetXY(66,$yPosition);
	if($dataRow['netWeight'] == 0)
	  $pdf->cell(18,05,"",'1', '0', 'L');
	else
	  $pdf->cell(18,05,$dataRow['netWeight'],'1', '0', 'R');
	
	$pdf->SetXY(84,$yPosition);
	if($dataRow['touch'] == 0)
	  $pdf->cell(20,05,"",'1', '0', 'L');
	else
	  $pdf->cell(20,05,$dataRow['touch'],'1', '0', 'R');
	
	$pdf->SetXY(104,$yPosition);
	if($dataRow['waste'] == 0)
	  $pdf->cell(15,05,"",'1', '0', 'L');
	else
	  $pdf->cell(15,05,$dataRow['waste'],'1', '0', 'R');
	
	$pdf->SetXY(119,$yPosition);
	if($dataRow['fine'] == 0)
	  $pdf->cell(15,05,"",'1', '0', 'L');
	else
	  $pdf->cell(15,05,(number_format($dataRow['fine'])),'1', '0', 'R');
	
  $pdf->SetXY(134,$yPosition);
  $pdf->cell(20,05,$dataRow['particular'],'1', '0', 'R');
  
	$pdf->SetXY(154,$yPosition);
	if($dataRow['qty'] == 0)
	  $pdf->cell(15,05,"",'1', '0', 'L');
	else
	  $pdf->cell(15,05,$dataRow['qty'],'1', '0', 'R');
	  
	$pdf->SetXY(169,$yPosition);
	if($dataRow['labour'] == 0)
	  $pdf->cell(20,05,"",'1', '0', 'L');
	else
	  $pdf->cell(20,05,$dataRow['labour'],'1', '0', 'R');
	  
	$pdf->SetXY(189,$yPosition);
	if($dataRow['amount'] == 0)
	  $pdf->cell(17,05,"",'1', '0', 'L');
	else
	  $pdf->cell(17,05,number_format($dataRow['amount']),'1', '0', 'R');
}
function totalBillDetail()
{
	global $pdf;
	global $totalNetWeight;
	global $totFine;
	global $totAmount;
	
	$pdf->SetXY(6,105);
	$pdf->cell(30,05,'Total','1', '0', 'L');
	$pdf->SetXY(66,105);
	$pdf->cell(18,05,$totalNetWeight,'1', '0', 'R');
	$pdf->SetXY(119,105);
	$pdf->cell(15,05,number_format($totFine),'1', '0', 'R');
  $pdf->SetXY(189,105);
	$pdf->cell(17,05,number_format($totAmount),'1', '0', 'R');
}
function remainRowFunction($yPosition)
{
	global $pdf;
	global $remainRow;
	$pdf->SetXY(6,$yPosition);
	$pdf->cell(30,05,'','1', '0', 'L');
	$pdf->SetXY(36,$yPosition);
	$pdf->cell(15,05,'','1', '0', 'L');
	$pdf->SetXY(51,$yPosition);
	$pdf->cell(15,05,'','1', '0', 'L');
	$pdf->SetXY(66,$yPosition);
	$pdf->cell(18,05,'','1', '0', 'L');
	$pdf->SetXY(84,$yPosition);
	$pdf->cell(20,05,'','1', '0', 'L');
	$pdf->SetXY(104,$yPosition);
	$pdf->cell(15,05,'','1', '0', 'L');
	$pdf->SetXY(119,$yPosition);
	$pdf->cell(15,05,'','1', '0', 'L');
  $pdf->SetXY(134,$yPosition);
  $pdf->cell(20,05,'','1', '0', 'L');
	$pdf->SetXY(154,$yPosition);
	$pdf->cell(15,05,'','1', '0', 'L');
	$pdf->SetXY(169,$yPosition);
	$pdf->cell(20,05,'','1', '0', 'L');
	$pdf->SetXY(189,$yPosition);
	$pdf->cell(17,05,'','1', '0', 'L');
}

function previousNet()
{
  global $pdf;
  global $previousFine;
  global $previousAmount;
  global $totalFine;
  global $totalAmount;
  
  $pdf->SetXY(85,114);
  $pdf->SetFont('Arial','',11);
  $pdf->Write(5,'Previous Fine : ');	 
  $pdf->SetXY(117,114);
  $pdf->cell(17,05,number_format($previousFine),'1', '0', 'R');

  $pdf->SetXY(150,114);
  $pdf->SetFont('Arial','',11);
  $pdf->Write(5,'Previous Amount : ');	 
  $pdf->SetXY(189,114);
  $pdf->cell(17,05,number_format($previousAmount),'1', '0', 'R');
  
  if($totalFine >= 0)
  {
    $pdf->SetXY(85,120);
    $pdf->SetFont('Arial','',11);
    $pdf->Write(5,'TotalDebitFine : ');	 
    $pdf->SetXY(117,120);
	  $pdf->cell(17,05,number_format($totalFine),'1', '0', 'R');
  }
  else
  {
    $pdf->SetXY(05,120);
    $pdf->SetFont('Arial','',11);
    $pdf->Write(5,'TotalCreditFine : ');
    $pdf->SetXY(117,120);
	  $pdf->cell(17,05,number_format($totalFine),'1', '0', 'R');
  }
  if($totalAmount >= 0)
  {
	  $pdf->SetXY(150,120);
    $pdf->SetFont('Arial','',11);
    $pdf->Write(5,'TotalDebitAmount : ');	 
	  $pdf->SetXY(189,120);
	  $pdf->cell(17,05,number_format($totalAmount),'1', '0', 'R');
	}
	else
	{
	  $pdf->SetXY(150,120);
    $pdf->SetFont('Arial','',11);
    $pdf->Write(5,'TotalCreditAmount : ');	 
	  $pdf->SetXY(189,120);
	  $pdf->cell(17,05,number_format($totalAmount),'1', '0', 'R');
  }
}
function border()
{
	global $pdf;
	$pdf->SetXY(03,03);
	$pdf->cell(205,130,'', '1');
}
?>